package com.jic.order.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderAfterSaleTime;
import com.jic.order.request.OrderAfterSaleTimeRequest;
import com.jic.order.response.OrderAfterSaleTimeResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderAfterSaleTimeService;
import com.jic.order.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "订单售后有效期设置", tags = {"订单售后有效期设置"})
@RequestMapping(value = "orderAfterSaleTime")
@RestController
public class OrderAfterSaleTimeController {

    @Autowired
    OrderAfterSaleTimeService orderAfterSaleTimeService;

    @ApiOperation(value = "售后有效期信息分页查询", notes = "分页查询售后有效期信息")
    @RequestMapping(value = "queryPage", method = RequestMethod.POST)
    public RestResult<PageResult<OrderAfterSaleTimeResponse>> queryPage(@RequestBody OrderAfterSaleTimeRequest form) {

        Page pageQuery = new Page();
        pageQuery.setLimit(form.getLimit());
        pageQuery.setStart(form.getStart());

        OrderAfterSaleTimeRequest orderAfterSaleTime = BeanUtil.copy(form, OrderAfterSaleTimeRequest.class);
        DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
        if (deptType.getCode() != 0) {
            orderAfterSaleTime.setDeptId(form.getDeptId());
        }
        PageResult<OrderAfterSaleTimeResponse> result= orderAfterSaleTimeService.queryPage(orderAfterSaleTime,pageQuery);

        List<OrderAfterSaleTimeResponse> orderAfterSaleTimeResponseList = result.getRows().stream().map(temp -> {
            OrderAfterSaleTimeResponse orderAfterSaleTimeResponse = BeanUtil.copy(temp, OrderAfterSaleTimeResponse.class);
            orderAfterSaleTimeResponse.setAfterSaleDateStr("订单已签收后" + orderAfterSaleTimeResponse.getAfterSaleDate() + "天");
            return orderAfterSaleTimeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<OrderAfterSaleTimeResponse> pageResult = new PageResult<>();
        pageResult.setRows(orderAfterSaleTimeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "售后有效期信息编辑/新增", notes = "售后有效期信息编辑/新增")
    @RequestMapping(value = "editOrderAfterSaleTime", method = RequestMethod.POST)
    public RestResult<Long> editOrderAfterSaleTime(@RequestBody OrderAfterSaleTime orderAfterSaleTime) {
        DeptTypeEnums deptType = orderAfterSaleTime.getAdminOperate().getDeptType();
        if (deptType.getCode() != 0) {
            orderAfterSaleTime.setDeptId(orderAfterSaleTime.getDeptId());
        }
        long result = 0;
        //修改
        if (orderAfterSaleTime.getId() > 0) {
            result = orderAfterSaleTimeService.update(orderAfterSaleTime);
        } else {//新增
            result = orderAfterSaleTimeService.insert(orderAfterSaleTime);
        }
        return RestResult.success(result);
    }

    @ApiOperation(value = "删除售后有效期信息", notes = "删除售后有效期信息/新增")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public RestResult<Long> delete(@RequestBody OrderAfterSaleTime orderAfterSaleTime) {
        return RestResult.success(orderAfterSaleTimeService.delete(orderAfterSaleTime.getId()));
    }

}
